<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class CreativeAction extends NewzanAction
{
    public function add()
    {
        $title = $this->_post('title');
        $adgroup_id = $this->_post('adgroup_id');
        $img_url = $this->_post('img_url');
        
        if( !$title || !$adgroup_id || !$img_url ){
            $this->error("创意必须要有一个标题和图片");
        }
        
        // add to the api
        $creativeMdl = M("Creative");
        $data = array(
            'title' => $title,
            'img_url' => $img_url,
            'adgroup_id' => $adgroup_id
        );
       
        $creative = $creativeMdl->add($this->me["user_id"] , $data);      
    }
    
    public function update()
    {
        $title = $_POST["title"];
        $img_url = $_POST["img_url"];
        $creative_id = $_POST["creative_id"];
        $adgroup_id = $_POST['adgroup_id'];
        $creatives = array();
        $creatives[] = array(
            'creative_id' => $creative_id[0],
            'title' => $title[0],
            'img_url' => $img_url[0],
            'adgroup_id' => $adgroup_id
        );
        
        $creatives[] = array(
            'creative_id' => $creative_id[1],
            'title' => $title[1],
            'img_url' => $img_url[1],
            'adgroup_id' => $adgroup_id            
        );
        
        $creativeMdl = D("Creative");
        try{
            foreach($creatives as $c){
                $creativeMdl->updateCreative($this->me["user_id"] , $c);
            }
        }catch(TopClientException $e){
            $error = getTopError($e->getMessage());
            $this->display_json( array("result" => false , "msg"=>$error ));
        }
        $this->display_json( array("result" => true) );
    }
}